<!-- 购物车底部计算 -->
<template>
  <div class="bottom-bar">
    <div class="checkBox">
      <check-botton 
      :is-checked='isSelectAll' 
      class="check-button"
      @click.native="checkClick" />
      <span>全选</span>
    </div>
    <div class="total-price">
    合计：{{totalPrice}}
    </div>
    <div class="calculate">
      去计算({{checkLength}})
    </div>
  </div>
</template>

<script>
import CheckBotton from 'components/content/checkButton/CheckBotton'

import { mapGetters } from 'vuex'

export default {
  name: 'cartBottomBar',
  data() {
    return {

    }
  },
  components:{
    CheckBotton
  },
  computed: {
    ...mapGetters(['cartList']),
    totalPrice(){
      return '￥' + this.$store.state.cartList.filter(item => {
        return item.checked
      }).reduce((preValue,item) => {
        return preValue + item.price * item.count
      }, 0).toFixed(2)
    },
    checkLength(){
      return this.$store.state.cartList.filter(item => item.checked).length
    },
    isSelectAll(){      
      if(this.cartList.length === 0) return false
      // return !(this.cartList.filter(item => !item.checked).length)

      // return !this.cartList.find(item => !item.checked)

      // return !(this.cartList.filter(item => !item.checked).length)
      
      for(let item of this.cartList){
        if(!item.checked){
          return false
        }
      }
      return true

    }

  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {

  },
  methods: {
    checkClick() {
      if(this.isSelectAll){ // 全部选中
        this.cartList.forEach(item => item.checked = false);
      } else {
        this.cartList.forEach(item => item.checked = true)
      }
    }
  },
}
</script>
<style scoped>
/* @import url(); 引入css类 */
.bottom-bar {
  display: flex;
  position: relative;
  height: 40px;
  bottom: 40px;  
  padding: 0 5px;
  background: rgb(243, 243, 243);
  box-sizing: border-box;
}
.checkBox {
  height: 40px;
  display: flex;
  align-items: center;
  /* line-height: 40px; */
}
.check-button {
  /* width: 20px;
  height: 20px; */
  /* line-height: 40px; */
}
.total-price {
  height: 40px;
  line-height: 40px;
  margin-left: 20px;
}
.calculate {
  /* display: flex; */
  position: absolute;
  right: 0;
  bottom: 0;
  float: right;
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  text-align: center;
  margin-left: 10px;
  align-items: center;
  color: #fff;
  background: #ff8198;
}
</style>
